# coding=utf-8

import txt2xlsx
import os
import sys
import xlwt
import warnings


default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

def __main__():
    path = u"C:\\Users\\Huang\\Documents\\code\\python\\DM\\data\\ceshi"
    prj_name = u"哈哈哈哈哈测量工程"
    measurer_name = u"这是谁"
    scale = u"1:500"
    tbl_maker = u"这是我"
    date = u"2016年1月2日"
    current_index = 0
    xls_creator = txt2xlsx.Excel_Creator(path)
    for filenames in os.walk(path):
        for filename in filenames[2]:
            filepath = path + u"\\" + filename
            loader = txt2xlsx.Load_txt(filepath)
            loader.Load()
            current_index = xls_creator.Write_Templete(current_index, prj_name,filename.split(".")[0],measurer_name,scale,tbl_maker,date)
            data_index = 1
            distance = 0.0
            xls_creator.HSheet.write(current_index - 4, 1, unicode(loader.x_left))
            xls_creator.HSheet.write(current_index - 4, 4, unicode(loader.y_left))
            xls_creator.HSheet.write(current_index - 3, 1, unicode(loader.x_right))
            xls_creator.HSheet.write(current_index - 3, 4, unicode(loader.y_right))
            for data in loader.data_list:
                if data_index == 1:
                    xls_creator.HSheet.write(current_index, 0, unicode(data_index))
                    xls_creator.HSheet.write(current_index, 1, u"0.0")
                    xls_creator.HSheet.write(current_index, 2, unicode(data[0]))
                    xls_creator.HSheet.write(current_index, 3, unicode(data[1]))
                    xls_creator.HSheet.write(current_index, 4, unicode(data[2]))
                else:
                    xls_creator.HSheet.write(current_index, 0, unicode(data_index))
                    xls_creator.HSheet.write(current_index, 1, (str(float(data[0]) - distance)))
                    xls_creator.HSheet.write(current_index, 2, unicode(data[0]))
                    xls_creator.HSheet.write(current_index, 3, unicode(data[1]))
                    xls_creator.HSheet.write(current_index, 4, unicode(data[2]))
                    distance = float(data[0])
                data_index = data_index + 1
                current_index = current_index + 1
            current_index = current_index + 2
            print filename + " finished"
            if loader.x_left == 0 or loader.y_right == 0 or loader.x_right == 0 or loader.y_left == 0:
                warnings.warn(filename + "基点坐标有误，请检查txt文件！")
    xls_creator.Save_Excel()



__main__()

